package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class SearchApiController {

    @Autowired
    private SearchService searchService;

    ///api/search/albumInfo
    @Operation(summary = "专辑搜索")
    @PostMapping
    public Result searchAlbums(@RequestBody AlbumIndexQuery query){
        AlbumSearchResponseVo vo =  searchService.searchAlbums(query);
        return Result.ok(vo);
    }

    //api/search/albumInfo/channel/2
    @Operation(summary = "首页根据1级分类id加载数据")
    @GetMapping("channel/{c1Id}")
    public Result getChannel(@PathVariable("c1Id")Long c1Id){
        List<Map> list = searchService.getChannel(c1Id);

        return Result.ok(list);
    }

    ///api/search/albumInfo/completeSuggest/？
    //只针对之后更新的数据才有效，因为之前的数据并没有添加到suggest中
    @Operation(summary = "首页搜索推荐")
    @GetMapping("completeSuggest/{keyword}")
    public Result<List<String>> completeSuggest(@PathVariable("keyword")String keyword){
        List<String> suggests =  searchService.completeSuggest(keyword);
        return Result.ok(suggests);
    }

    @Operation(summary = "查询专辑详情")
    @GetMapping("{albumId}")
    public Result getAlbumInfoById(@PathVariable("albumId")Long albumId){
        return Result.ok(searchService.getAlbumInfoById(albumId));
    }

    //api/search/albumInfo/findRankingList/1/playStatNum
    @Operation(summary = "获取专辑排行榜")
    @GetMapping("findRankingList/{c1Id}/{sort}")
    public Result findRankingList(@PathVariable Long c1Id,
                                  @PathVariable String sort){
      List<AlbumInfoIndexVo> albumInfoIndexVolist = searchService.findRankingList(c1Id,sort);
        return Result.ok(albumInfoIndexVolist);
    }

    @Operation(summary = "预热热门专辑排行数据")
    @GetMapping("updateLatelyAlbumRanking")
    public Result updateLatelyAlbumRanking(){
        searchService.updateLatelyAlbumRanking();
        return Result.ok();
    }

}

